using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using Dragon.Prototype1.Infrastructure;
using Dragon.Prototype1.Infrastructure.FormBuilder;
using Dragon.Prototype1.Service;
using Dragon.Prototype1.Infrastructure.Controls;
using LINQtoSQLDataProvider;

namespace Dragon.Prototype1.Web.Controllers
{
    public class WarehouseController : Controller
    {
        //
        // GET: /Warehouse/

        public ActionResult Index()
        {
            var data = WarehouseService.GetRootWarehouses();
            Warehouse firstws = data.First();
            ViewData["rootid"] = firstws.WarehouseID.ToString();
            ViewData["needIns"]= true;
            return View(data);
        }
        public ActionResult DetailView(Guid id)
        {
            if (id == Guid.Empty)
            {
                return RedirectToAction("Index");
            }
            Warehouse wh = WarehouseService.GetWarehouse(id);
            jQueryFormBuilder formbuilder = new jQueryFormBuilder();
            formbuilder.CssClass = "tabcontainer";
            FormGroup formgroup1 = new FormGroup("MAINGROUP");
            formgroup1.ColumnNumber = 2;

            formgroup1.AddLabeledLiteral(this.Resource("Warehouse", "WAREHOUSECODE"), wh.WarehouseCode,false);
            formgroup1.AddLabeledLiteral(this.Resource("Warehouse", "WAREHOUSENAME"), wh.WarehouseName,false);
            formgroup1.AddLabeledOptionText("warehouse-type", wh.NodeType.ToString(), false);
            formgroup1.AddLabeledLiteral(this.Resource("Warehouse", "PARENTWAREHOUSE"),wh.Parent.WarehouseName,false);
            formgroup1.AddLabeledLiteral(this.Resource("Global", "DESCRIPTION"), wh.Description, true);
            formgroup1.AddLabeledBooleanText(this.Resource("Warehouse", "HASBATCHNO"), wh.HasBatchNo,true);
            formgroup1.AddLabeledBooleanText(this.Resource("Warehouse", "HASAREA"), wh.HasArea, true);
            formgroup1.AddLabeledBooleanText(this.Resource("Warehouse", "HASLOCATION"), wh.HasLocation, true);

            formbuilder.Groups.Add(formgroup1);
            WarehouseService.DisposeDataContext();
            return View(formbuilder);
        }

        private jQueryFormBuilder CreateForm()
        {
            jQueryFormBuilder formbuilder = new jQueryFormBuilder();
            formbuilder.CssClass = "tabcontainer";
            FormGroup formgroup1 = new FormGroup("MAINGROUP");
            formgroup1.ColumnNumber = 2;

            formgroup1.AddLabeledTextBox("warehouseCode", this.Resource("Warehouse", "WAREHOUSECODE"), true);
            formgroup1.AddLabeledTextBox("warehouseName", this.Resource("Warehouse", "WAREHOUSENAME"), true);
            formgroup1.AddLabeledDropDownList("warehouseType", "warehouse-type", true, false);
            PopupBox pb=new PopupBox("parentWarehouse");
            pb.FormName="form1";
            pb.ModuleName="Warehouse";
            pb.PopupWidth=300;
            pb.PopupHeight=400;
            pb.FieldToControlNameMapping.Add("name", pb.TextBoxID);
            pb.FieldToControlNameMapping.Add("id", pb.HiddenBoxID);

            formgroup1.AddLabeledPopupBox(pb.TextBoxID, this.Resource("Warehouse", "PARENTWAREHOUSE"),pb, true,false);
            formgroup1.AddLabeledTextBox("desc", this.Resource("Global", "DESCRIPTION"),string.Empty,string.Empty,false,true,true,false);
            formgroup1.AddLabeledCheckbox("hasBatchNo",this.Resource("Warehouse", "HASBATCHNO"),false,true,true);
            formgroup1.AddLabeledCheckbox("hasArea",this.Resource("Warehouse", "HASAREA"),false,true,true);
            formgroup1.AddLabeledCheckbox("hasLocation",this.Resource("Warehouse", "HASLOCATION"),false,true,true);

            formbuilder.Groups.Add(formgroup1);
            return formbuilder;
        }
        public ActionResult ListData(Guid warehouseid,string sidx, string sord, int page, int rows,string warehouseCode,string warehouseName)
        { 
            string sortString = string.Format("a.{0} {1}", sidx, sord.ToUpper());

            var warehouses=WarehouseService.GetChildren(warehouseid, page, rows, sortString, warehouseCode, warehouseName);
            int totalnum = warehouses.Count;
            var jsonData = new
            {
                total = totalnum / rows + 1,
                page = page,
                records = totalnum,
                rows = (from row in warehouses
                        select new
                        {
                            id = row.WarehouseID.ToString(),
                            cell = new string[]
                            {
                                row.WarehouseID.ToString(),
                                row.WarehouseCode,
                                row.WarehouseName,
                                (
                                row.NodeType=='A'? ResourceHtmlHelpers.Resource("DataDictionary","WAREHOUSEAREA"):
                                row.NodeType=='W'? ResourceHtmlHelpers.Resource("DataDictionary","WAREHOUSE"):
                                row.NodeType=='L'? ResourceHtmlHelpers.Resource("DataDictionary","WAREHOUSELOCATION"):
                                "Unknown"
                                ),
                                (
                                row.HasBatchNo? ResourceHtmlHelpers.Resource("DataDictionary","YES"):ResourceHtmlHelpers.Resource("DataDictionary","NO")
                                )
                                
                            }
                        }).ToArray()
            };

            return Json(jsonData);
        }
        
        public ActionResult Create() 
        {
            //ViewData["pid"] = id.ToString();
            return View(CreateForm());
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(string warehouseCode, string warehouseName, char warehouseType,
            Guid parentWarehouse_hidden, string desc,
            string hasLocation, string hasArea, string hasBatchNo)
        {
            bool bHasLocation = Util.GetCheckBoxValue(hasLocation);
            bool bHasArea = Util.GetCheckBoxValue(hasArea);
            bool bHasBatchNo = Util.GetCheckBoxValue(hasBatchNo);

            Guid userid = UserProfile.GetUserId(User.Identity.Name);

            if (WarehouseService.CreateWarehouse(warehouseCode, warehouseName, warehouseType, parentWarehouse_hidden, desc, bHasLocation, bHasArea, bHasBatchNo,userid))
            {
                return RedirectToAction("Index");
            }
            else
            {
                ViewData["ErrorMessage"] = "Cannot create the warehouse, please double-check your data and submit again";
                return View("Create", CreateForm());
            }  
        }

        public ActionResult Edit(Guid id)
        {
            if (id == Guid.Empty)
            {
                return RedirectToAction("Index");
            }
            Warehouse wh = WarehouseService.GetWarehouse(id);

            jQueryFormBuilder formbuilder = new jQueryFormBuilder();
            formbuilder.CssClass = "tabcontainer";
            FormGroup formgroup1 = new FormGroup("MAINGROUP");
            formgroup1.ColumnNumber = 2;

            formgroup1.AddLabeledTextBox("warehouseCode", this.Resource("Warehouse", "WAREHOUSECODE"),wh.WarehouseCode, true);
            formgroup1.AddLabeledTextBox("warehouseName", this.Resource("Warehouse", "WAREHOUSENAME"),wh.WarehouseName, true);
            formgroup1.AddLabeledDropDownList("warehouseType", "warehouse-type",wh.NodeType.ToString(), true, false);
            PopupBox pb = new PopupBox("parentWarehouse");
            pb.FormName = "form1";
            pb.ModuleName = "Warehouse";
            pb.PopupWidth = 300;
            pb.PopupHeight = 400;
            pb.Text=wh.Parent.WarehouseName;
            pb.HiddenValue = wh.ParentID.ToString();
            pb.FieldToControlNameMapping.Add("name", pb.TextBoxID);
            pb.FieldToControlNameMapping.Add("id", pb.HiddenBoxID);

            formgroup1.AddLabeledPopupBox(pb.TextBoxID, this.Resource("Warehouse", "PARENTWAREHOUSE"), pb, true, false);
            formgroup1.AddLabeledTextBox("desc", this.Resource("Global", "DESCRIPTION"),wh.Description, string.Empty, false, true, true, false);
            formgroup1.AddLabeledCheckbox("hasBatchNo", this.Resource("Warehouse", "HASBATCHNO"),wh.HasBatchNo, false, true);
            formgroup1.AddLabeledCheckbox("hasArea", this.Resource("Warehouse", "HASAREA"),wh.HasArea, false, true);
            formgroup1.AddLabeledCheckbox("hasLocation", this.Resource("Warehouse", "HASLOCATION"),wh.HasLocation, false, true);

            formbuilder.Groups.Add(formgroup1);
            WarehouseService.DisposeDataContext();
            return View(formbuilder);
        }

        public ActionResult Popup()
        {
            var data = WarehouseService.GetRootWarehouses();
            ViewData["needIns"] = true;
            return View(data);            
        }
        
        public ActionResult Delete(Guid id)
        {
            return Json(WarehouseService.DeleteWarehouses(id));
        }
    }
}
